package com.ln.prac.lianglianglee.jmx;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class UserJmxServer {

    public static void main(String[] args) {
        MBeanServer server;
        User bean = new User();

        try {

            int rmiPort = 1099;
            String jmxServerName = "TestJMXServer";

            Registry registry = LocateRegistry.createRegistry(rmiPort);
            server = MBeanServerFactory.createMBeanServer("user");

            ObjectName objectName = new ObjectName("user:name=User");
            server.registerMBean(bean, objectName);

            JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/user");
            System.out.println("JMXServiceURL: " + url.toString());
            JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
            jmxConnServer.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
